<?php
require APPPATH.'libraries/ApiController.php';
class shop extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	//获取用户名下店铺列表
	public function list(){
		$token=$this->verifyToken();
		if($token){
			$this->db->where("uid",$token["uid"]);
			$this->db->order_by("default","desc");
			$shops=$this->db->get("shop")->result_array();

			for($i=0;$i<count($shops);$i++){
				$shops[$i]["default"]=$shops[$i]["default"]=="0"? false:true;
				$shops[$i]["logo"]=$this->config->item("domain").$shops[$i]["logo"];
			}

			$this->WRITE("code",1);
			$this->WRITE("list",$shops);
			$this->RESPONSE();
		}
		
	}
	
	//获取店铺详情
	public function detail(){
		$token=$this->verifyToken();
		if($token){
			$this->db->where("shopid",$token["shopid"]);
			$shop=$this->db->get("shop")->row_array();
			if($shop["logo"]!=""){
				$shop["logo"]=$this->config->item("domain").$shop["logo"];
			}
			$this->WRITE("code",1);
			$this->WRITE("data",$shop);
			$this->RESPONSE();
		}
	}
	public function create(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["title",true,"店铺名称",""],
			["classify",false,"行业","通用"],
			["leader",false,"负责人",""],
			["mobile",false,"联系电话",""],
			["logo",false,"店铺Logo",""],
			["address",false,"店铺地址",""]
			],'post');

			$data["uid"]=$token["uid"];
			
			$this->db->select("uid,mobile,nickname,password,admin.shopid,avatar,gender,admin.roleid,roletitle,admin.shopid");
			$this->db->join("admin_role","admin_role.roleid=admin.roleid","left");
			$this->db->where("uid",$token["uid"]);
			$user=$this->db->get("admin")->row_array();
			
			//上传头像
			if (!file_exists($this->config->item("upload")."/uploads/shoplogo/")) {
			    mkdir($this->config->item("upload")."/uploads/shoplogo/", 0777, true);
			}
			
			foreach ($_FILES as $key => $file) {
				if (strpos($file["type"], 'image') !== false) {
					$ext = substr(strrchr($file["type"], "/"), 1);
					$filename = 'shop_logo_'.uniqid().'.'.$ext;
					$path='/uploads/shoplogo/'.$filename;
					if (move_uploaded_file($file["tmp_name"],$this->config->item("upload").$path)) {
						//上传成功
						$data["logo"]=$path;
					}
				}
			}
			
			$shop=$data;
			$this->db->insert("shop",$data);
			$shopid=$this->db->insert_id();
			$shop["shopid"]=$user["shopid"]=$shopid;
			//更新用户表中shopid;
			$this->db->where("uid",$token["uid"])->update("admin",array("shopid"=>$shopid));

			//构建token
			$tokendata=array(
				"uid"=>$user["uid"],
				"mobile"=>$user["mobile"],
				"nickname"=>$user["nickname"],
				"gender"=>$user["gender"],
				"avatar"=>$user["avatar"],
				"roleid"=>$user["roleid"],
				"roletitle"=>$user["roletitle"],
				"shopid"=>$shop==null? -1:$shop["shopid"]
			);
			
			$token=$this->createToken($tokendata);
			
			$this->WRITE("code",1);
			$this->WRITE("userinfo",$user);
			$this->WRITE("token",$token);
			
			//更新店铺logo
			$shop["logo"]=$shop["logo"]==""? "": $this->config->item("domain").$shop["logo"];
			$this->WRITE("shop",$shop);
			$this->WRITE("msg","创建店铺成功");
			$this->RESPONSE();
		}
	}
	
	
	public function update(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["shopid",true,"店铺ID"],
			["title",true,"店铺名称",""],
			["classify",false,"行业","通用"],
			["leader",false,"负责人",""],
			["mobile",false,"联系电话",""],
			["logo",false,"店铺Logo",""],
			["address",false,"店铺地址",""]
			],'post');
			
			//如果有上传的文件
			if($_FILES){
				//删除头像
				$oldlogo=$this->db->where("shopid",$data["shopid"])->get("shop")->row_array()["logo"];
				if($oldlogo!=""){
					if(file_exists($this->config->item("upload").$oldlogo)){
						unlink($this->config->item("upload").$oldlogo);
					}
				}
				
				if (!file_exists($this->config->item("upload")."/uploads/shoplogo/")) {
				    mkdir($this->config->item("upload")."/uploads/shoplogo/", 0777, true);
				}
				
				//保存上传头像
				foreach ($_FILES as $key => $file) {
					if (strpos($file["type"], 'image') !== false  ) {
						$ext = pathinfo($file["name"],PATHINFO_EXTENSION);
						$filename = 'logo_'.uniqid().'.'.$ext;
						$path='/uploads/shoplogo/'.$filename;
						if (move_uploaded_file($file["tmp_name"],$this->config->item("upload").$path)) {
							//上传成功
							$data["logo"]=$path;
						}
					}
				}
			}else{
				$data["logo"]=str_replace($this->config->item("domain"),"",$data["logo"]);
			}
			$this->db->where("shopid",$data["shopid"]);
			$this->db->update("shop",$data);
			
			$shop=$this->db->where("shopid",$data["shopid"])->get("shop")->row_array();
			
			$shop["logo"]=$shop["logo"]==""? "":$this->config->item("domain").$shop["logo"];
			$this->WRITE("code",1);
			$this->WRITE("shop",$shop);
			$this->WRITE("msg","编辑店铺信息成功");
			$this->RESPONSE();
		}
	}
	//切换默认店铺
	public function switchshop(){
		$token=$this->verifyToken();
		if($token){
		$data=$this->getKeys([
			["shopid",true,"店铺ID"],
			["title",true,"店铺名称"]],'post');
			
			//当前用户人默认店铺重置
			$this->db->where("uid",$token["uid"]);
			$this->db->update("shop",array("default"=>0));
			//设置当前店铺为默认
			$this->db->where("shopid",$data["shopid"]);
			$this->db->update("shop",array("default"=>1));
			
			$res=$this->db->where("uid",$token["uid"])->update("admin",array("shopid"=>$data["shopid"]));
			
			$this->db->select("uid,mobile,nickname,password,shopid,avatar");
			$this->db->where("uid",$token["uid"]);
			$user=$this->db->get("admin")->row_array();
			
			$userinfo=array(
				"uid"=>$user["uid"],
				"mobile"=>$user["mobile"],
				"nickname"=>$user["nickname"],
				"avatar"=>$user["avatar"],
				"shopid"=>$data["shopid"]);
			$token=$this->createToken($userinfo);
			$this->WRITE("userinfo",$userinfo);
			$this->WRITE("shop",$data);
			$this->WRITE("token",$token);		
			$this->WRITE("code",1);
			$this->WRITE("msg","切换店铺成功!");
			$this->RESPONSE();	
		}
	}
}

	